// 用户数据持久化
import {setStore, getStore, removeStore} from '@/util/store'
import {setToken, getToken, removeToken} from '@/util/auth'
import {validatenull} from '@/util/validate'
import {encryption} from '@/util/util'
import {loginByUsername, getMenu, getUsers, getRoles, getUserInfo} from '@/page/api/user'
const user = {
	state:{
		menu:[],
		userInfo:{},
		users:[],
		roles:[],
		token: getStore({ name: 'token' }) || '',
	},
	actions:{
		// 根据用户名登录
		LoginByUsername({commit, state, dispatch},userInfo){
			return new Promise((resolve, reject)=>{
				loginByUsername(userInfo.email,userInfo.password,userInfo.code).then(res=>{
					const data = res.data;
					dispatch('GetMenu');
					commit('SET_TOKEN', data);
					setToken(data);
					resolve();
				})
			})
		},
		// 获取用户菜单
		GetMenu({commit}){
			return new Promise((resolve, reject)=>{
				getMenu().then(res=>{
					const data = res.data;
					commit('SET_MENU', data);
					resolve();
				})
			})
		},
		// 获取用户列表
		GetUsers({commit}){
			return new Promise((resolve, reject)=>{
				getUsers().then(res=>{
					const data = res.data;
					commit('SET_USERS', data);
					resolve();
				})
			})
		},
		// 获取角色列表
		GetRoles({commit}){
			return new Promise((resolve, reject)=>{
				getRoles().then(res=>{
					const data = res.data;
					commit('SET_ROLES', data);
					resolve();
				})
			})
		},
	},
	mutations:{
		SET_TOKEN: (state, token) => {
            state.token = token;
            setStore({ name: 'token', content: state.token, type: 'session' })
        },
		SET_MENU: (state, menu) => {
            state.menu = menu;
        },
        SET_USERS: (state, users) => {
        	state.users = users;
        },
        SET_ROLES: (state, roles) => {
        	state.roles = roles;
        }
	}
}

export default user